<?php
@session_start();
set_time_limit(0);
require_once 'lib/amazon/amazon_api_class.php';
require_once 'lib/content.php';
require_once 'lib/xmlrpc.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>โปรแกรม cWPress version 1.0.0 By Channgam</title>
    </head>
    <body>
        <h1>โปรแกรม cWPress version 1.0.0 By Channgam</h1>

        <h2>โปรแกรมโพส Wordpress ด้วย ASIN</h2>
        <?php
        if($_POST['hidden']=='Y') {
			$asso       = trim($_POST['asso']);
            $public     = trim($_POST['public']);
            $private    = trim($_POST['private']);
			$keyword    = trim($_POST['keyword']);

			$wp_url = trim($_POST['wp_url']);
			$wp_username = trim($_POST['wp_username']);
			$wp_password = trim($_POST['wp_password']);
			$wp_category = trim($_POST['wp_category']);
			$wp_tags = trim($_POST['wp_tags']);

            $obj_amazon = new AmazonProductAPI();
            $obj_amazon->setAssociateTag($asso);
            $obj_amazon->setPublicKey($public);
            $obj_amazon->setPrivateKey($private);

            try {
                echo '<div>...Starting connect to Amazon...</div>';
                flush();

                /* Load XML Template Config */

                $config = @simplexml_load_file("template.xml");

                $splite_key = explode(":", $keyword);
                $search = trim($splite_key[0]);
                $keyword = trim($splite_key[1]);

                $xml = $obj_amazon->getItemByAsin($search);
                echo '<div>...Connected to amazon...</div>';
                flush();

                $param = array();
                $param['title']   = base64_decode($config->title);
                $param['content'] = base64_decode($config->content);
                $param['keyword'] = $keyword;

                $post_data = Content::getData($param , $xml);
                
				$content = array(
                            'title'=>$post_data['title'],
                            'description'=>$post_data['content'],
                            'mt_allow_comments'=>0,  // 1 to allow comments
                            'mt_allow_pings'=>0,  // 1 to allow trackbacks
                            'post_type'=>'post',
                            'mt_keywords'=>$wp_tags,
                            'categories'=>array($wp_category)
                );

                    // Create the client object
                    $wp_url = $wp_url.'/xmlrpc.php';
                    $client = new IXR_Client($wp_url);

                    $params = array(0,$wp_username,$wp_password,$content,true);

                    // Run a query for PHP
                    if (!$client->query('metaWeblog.newPost', $params)) {
                        echo '<div style="color:red">***'.$client->getErrorCode().' : '.$client->getErrorMessage().'***</div>';
                    }else {
                        echo  '<div style="color:green">***Posted '.$search.' Successfully***</div>';
                        flush();
                    }

            }catch (Exception $e) {
                echo '<div style="color:red">***'.$e->getMessage().'***</div>';
                flush();
            }
        }
        ?>
        <form action="" method="post">
            <fieldset>
                <input type="hidden" id="hidden" name="hidden" value="Y" />
                <fieldset style="background: #F2F2F2;"><legend><h3>Wordpress Config</h3></legend>
                    <table>
                        <tr>
                            <td>Wordpress URL :</td>
                            <td>
                                <input type="text" id="wp_url" name="wp_url" size="50" />
                                <i>ตัวอย่าง เช่น http://www.xxx.com</i>
                            </td>
                        </tr>
                        <tr>
                            <td>Wordpress Username :</td>
                            <td>
                                <input type="text" id="wp_username" name="wp_username" size="50" />
                            </td>
                        </tr>
                        <tr>
                            <td>Wordpress Password :</td>
                            <td>
                                <input type="text" id="wp_password" name="wp_password" size="50" />
                            </td>
                        </tr>
                        <tr>
                            <td>Wordpress Category :</td>
                            <td>
                                <input type="text" id="wp_categoryl" name="wp_category" size="50" />
                                <i>ตัวอย่าง เช่น category1, category2</i>
                            </td>
                        </tr>
                        <tr>
                            <td>Wordpress Tags :</td>
                            <td>
                                <input type="text" id="wp_tags" name="wp_tags" size="50" />
                                <i>ตัวอย่าง เช่น tag1, tag2</i>
                            </td>
                        </tr>
                    </table>
                </fieldset>

                <fieldset style="background: #F2F2F2;"><legend><h3>Amazon Config</h3></legend>
                    <table>
                        <tr>
                            <td>Associate Tag :</td>
                            <td><input type="text" id="asso" name="asso" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Public Key :</td>
                            <td><input type="text" id="public" name="public" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Private Key :</td>
                            <td><input type="text" id="private" name="private" size="50" /></td>
                        </tr>
                        <tr>
                            <td>ASIN :</td>
                            <td>
                                <input type="text" id="keyword" name="keyword" size="50" />
                                <i>วิธีการใส่ ASIN และ Keyword เช่น  B0002GXZK4:keyword</i>
                            </td>
                        </tr>
                    </table>
                </fieldset>
            </fieldset>
			<input type="submit" value="โพสข้อมูล" />
        </form>
        <a href ="template.php">ตั้งค่า Template</a>
    </body>
</html>
